package com.lb.bs.demo;

import com.alibaba.fastjson.JSON;
import com.lb.bs.client.adapter.simple.SimpleStarter;
import com.lb.bs.client.config.StaticConfig;
import com.lb.bs.client.repository.BeanHolder;
import com.lb.bs.client.util.TraceThreadPoolExecutor;
import com.lb.bs.demo.config.Confuigration;
import com.lb.bs.demo.testBean.Hello;
import com.lb.bs.demo.testBean.Student;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * program: LB-BS-Client
 * author: bsworld.xie
 * create: 2019-11-28 19:26
 * description:
 */
@Component
public class ClientStart {

    @Autowired

    private static final Logger logger = LoggerFactory.getLogger(ClientStart.class);


    public static void main(String[] args) throws IOException, InterruptedException, NoSuchMethodException {
//        logger.info("start test");
//        System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "/Users/xieziyang/projects/lb-bs-client/Demo/src/main/java/com/lb/bs/demo");
        testSpringLb();
    }


    private static void testSimpleLb() throws InterruptedException {
        StaticConfig staticConfig = StaticConfig.builder()
                .zkHost("106.13.46.179:2181")
                .scanPackages("com.lb.bs.demo")
                .build();
        new SimpleStarter()
                .appendStaticConfig(staticConfig)
                .start();

        Thread.sleep(1000);
        Student student = (Student) BeanHolder.getHolder().getProxyBeanByOriginClazzType(Student.class);
        while (true) {
            Thread.sleep(100);
            System.out.println(JSON.toJSONString(student));
        }
    }

    private static void testSpringLb() throws InterruptedException, NoSuchMethodException {
        AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext();
        ac.register(Confuigration.class);
        ac.scan("com.lb.bs.demo");
        ac.refresh();
        Thread.sleep(1000);

        Student student = ac.getBean(Student.class);
        Hello hello = ac.getBean(Hello.class);
        while (true) {
            Thread.sleep(500);
//            cacheVal = LBCache.getCacheVal(originStudent.getClass(), method.getAnnotation(LBItem.class));
//            System.out.println(cacheVal);
            try {
                String name = student.getName();
                logger.info("info, name:{}", name);
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
    }


    private static void testLbClientDestroy(AnnotationConfigApplicationContext ac) throws IOException {
        DefaultSingletonBeanRegistry registry = (DefaultSingletonBeanRegistry) ac.getBeanFactory();
        registry.destroySingleton("lBSecondBean");
        System.in.read();
    }


    private static void testLogTrace() {
        MDC.put("traceId", "100033");

        TraceThreadPoolExecutor executor = new TraceThreadPoolExecutor(10, 10, 10, TimeUnit.SECONDS, new ArrayBlockingQueue<>(100));
        executor.execute(() -> {

            logger.info("start run");
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            logger.info("end run");
        });
    }


}
